﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="List.aspx.cs" Inherits="WineCellar.Web.Views.Wine.List" %>
<%@ Import Namespace="WineCellar.Web.Helpers"%>
<%@ Import Namespace="WineCellar.Web.Resources"%>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
    $(document).ready(function() {
        $("#WineAddButton").click(function() {
            $.get(baseUrl + "Wine/New",function(data) {
                LoadContainer(data, true, "WineEditContainer");
            });
        });


    });

function LoadContainer(data, isNew, container) {
    var container = $("#" + container);
    container.empty();
    container.append(data);
    container.show();

    $("#WineEditSaveButton").click(function() {
        SubmitForm("WineForm", isNew);
    });

    $("#WineEditCancelButton").click(function() {
        container.slideUp("fast");
        container.empty();
    });
}
function SubmitForm(formId, isNew) {
    var form = $("#" + formId);
    if (form.valid()) {
        var action;
        if (isNew) {
            action = baseUrl + "Wine/Create";
        }
        else {
            var assignmentId = grid.getSelectionModel().getSelected().data["Id"].toString();
            action = baseUrl + "Wine/Update" + "/" + assignmentId;
        }
        var serializedForm = form.serialize();

        $.ajax({
            type: "POST",
            url: action,
            data: serializedForm,
            success: function() {
                $("#WineEditContainer").slideUp("fast");
                grid.getStore().reload();
            },
            error: function(XMLHttpRequest, textstatus, errorThrown) {
                $("#WineFormErrorsummary").html(XMLHttpRequest.responseText).show("fast");
            }
        });
    }
}
</script>

<%var model = new ExtJsGridModel();
  model.Add("Name", Strings.Name);
  model.Add("Producer", Strings.Producer);
  model.Add("Appelation", Strings.Appelation);
  model.Add("Score", Strings.Score);
%>

<%= Html.ExtJsGrid("wineList","Name","Wine/GetWines",model.ExtRecords,model.ExtFilters,model.ExtColumnModels,"List of wines",Strings.ResourceManager,"grid") %>

<div id="wineList"></div>
<!--Buttons-->
<div>
    <input type="button" id="WineAddButton" value="Add"/>
    <input type="button" id="WineEditButton" value="Edit"/>
</div>

<div id="WineEditContainer"></div>



</asp:Content>
